<template>
  <div class="table">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i>训练管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-select v-model="select_status" placeholder="筛选状态" class="handle-select mr10" @change="selectUserStatus">
          <el-option key="1" label="启用" value="1"></el-option>
            <el-option key="2" label="禁用" value="0"></el-option>
              <el-option key="3" label="全部" value="-1"></el-option>
        </el-select>
        <el-input v-model="select_word" placeholder="筛选关键词" class="handle-input mr10"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button type="primary" icon="el-icon-lx-add" @click="addVisible = true">添加训练</el-button>
      </div>
      <el-table
        :data="data.slice((currentPage-1)*pagesize,currentPage*pagesize)"
        border
        class="table"
        ref="multipleTable"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="index" width="80" align="center" sortable label="序列"></el-table-column>
        <el-table-column prop="trainname" label="训练名称" width="100"></el-table-column>
        <el-table-column prop label="训练海报" width="100">
          <template slot-scope="scope">
            <el-image :src="`http://localhost/fitness/public/static/img/`+scope.row.tpic"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="intro" label="训练简介" width="100"></el-table-column>
        <el-table-column prop="traindate" label="训练日期" sortable width="200"></el-table-column>
        <el-table-column prop="clubname" label="所属俱乐部" width="auto"></el-table-column>
        <el-table-column prop="trainaddress" label="训练地址" width="200"></el-table-column>
        <el-table-column prop="price" label="训练价格" width="80"></el-table-column>
        <el-table-column label="查看此训练订单" width="150" align="center">
          <template slot-scope="scope">
            <router-link :to="'/trainorder'+ scope.row.trainid" style="color:#ff6348">
              <el-button style="color:#ff6348" @click="newdate">查看</el-button>
            </router-link>
          </template>
        </el-table-column>
        <el-table-column prop="tstatus" label="训练状态" width="100" align="center">
          <template slot-scope="scope">
            <template v-if="scope.row.tstatus===1">
              <i class="el-icon-circle-check" style="color:#1e90ff">已启用</i>
            </template>
            <template v-else>
              <i class="el-icon-circle-close" style="color:red">未发布</i>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <template v-if="scope.row.tstatus===1">
              <el-button
                type="text"
                icon="el-icon-delete"
                class="red"
                @click="handleBan(scope.row)"
              >禁用</el-button>
            </template>
            <template v-else>
              <el-button type="text" icon="el-icon-circle-check" @click="handleOn(scope.row)">启用</el-button>
              <el-button type="text" icon="el-icon-edit" @click="onclickEdit(scope.row)">修改</el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 8, 20, 40]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="trainData.length"
        ></el-pagination>
      </div>
    </div>

    <!-- 添加训练弹出框 -->
    <el-dialog title="请填训练信息" :visible.sync="addVisible" width="30%">
      <el-form ref="addform" :model="addform" :rules="rules" label-width="80px">
        <el-form-item label="训练名称" prop="trainname">
          <el-input v-model="addform.trainname"></el-input>
        </el-form-item>
        <el-form-item label="训练海报">
          <el-upload
            ref="upload"
            class="upload-demo"
            action="/api/Train/addtrain"
            name="pic"
            :file-list="fileList"
            :limit="1"
            list-type="picture"
            :on-success="handlePicsuccess"
            :auto-upload="false"
            :on-change="handleChange"
            :on-remove="handleRemove"
            :data="addform"
          >
            <el-button size="small" type="primary" @click="piclimit">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="训练地点" prop="trainaddress">
          <el-input v-model="addform.trainaddress"></el-input>
        </el-form-item>
        <el-form-item label="训练简介" prop="intro">
          <el-input v-model="addform.intro"></el-input>
        </el-form-item>
        <el-form-item hidden prop="clubid">
          <el-input v-model="clubid" hidden></el-input>
        </el-form-item>
        <el-form-item label="训练时间" prop="traindate">
          <el-date-picker v-model="addform.traindate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="训练价格" prop="price">
          <el-input v-model="addform.price"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAdd">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑弹出框 -->
    <el-dialog title="修改训练信息" :visible.sync="editVisible" width="30%">
      <el-form ref="form" :model="form" :rules="rules1" label-width="80px">
        <el-form-item label="训练名称" prop="trainname">
          <el-input v-model="form.trainname"></el-input>
        </el-form-item>
        <el-form-item label="训练海报">
          <el-upload
            ref="upload"
            class="upload-demo"
            action="/api/Train/edittrain"
            name="pic"
            :file-list="fileListEdit"
            :limit="1"
            list-type="picture"
            :on-success="handlePicsuccess"
            :auto-upload="false"
            :data="form"
          >
            <el-button size="small" type="primary" @click="piclimit">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="训练地点" prop="trainaddress">
          <el-input v-model="form.trainaddress"></el-input>
        </el-form-item>
        <el-form-item label="训练时间" prop="traindate">
          <el-date-picker v-model="form.traindate" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="训练价格" prop="price">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item label="训练简介" prop="intro">
          <el-input v-model="form.intro"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 禁用提示框 -->
    <el-dialog title="提示" :visible.sync="banVisible" width="300px" center>
      <div class="del-dialog-cnt">确定禁用该俱乐部？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="banVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmBan">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 启用提示框 -->
    <el-dialog title="提示" :visible.sync="onVisible" width="300px" center>
      <div class="del-dialog-cnt">确定启用该俱乐部？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmOn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { log } from "util";
export default {
  name: "basetable",
  data() {
    return {
      currentPage: 1, //初始页
      pagesize: 5, //    每页的数据
      // userList: [],
      select_status: '全部',
      multipleSelection: [],
      select_cate: "",
      select_word: "",
      is_search: false,
      addVisible: false,
      editVisible: false,
      banVisible: false,
      onVisible: false,
      trainData: [],
      trainClub: [],
      fileList: [],
      fileListEdit: [],
      addform: {
        trainname: "",
        intro: "",
        trainaddress: "",
        traindate: "",
        price: "",
        clubid: "",
      },
      form: {
        trainname: "",
        intro: "",
        trainaddress: "",
        traindate: "",
        price: "",
        clubid: ""
      },
      rules: {
        trainname: [
          {
            required: true,
            message: "训练名称不能为空",
            trigger: "blur"
          },
          {
            pattern: /^[\u4E00-\u9FA5]+$/,
            message: "训练只能为中文",
            trigger: "blur"
          }
        ],
        // clubid: [
        //   {
        //     required: true,
        //     message: "请选择属俱乐部",
        //     trigger: "change"
        //   }
        // ],
        trainaddress: [
          {
            required: true,
            message: "请填写训练地址",
            trigger: "blur"
          }
        ],
        intro: [
          {
            required: true,
            message: "训练简介不能为空",
            trigger: "blur"
          }
        ],
        traindate: [
          {
            type: "string",
            required: true,
            message: "请选择训练时间",
            trigger: "blur"
          }
        ],
        price: [
          {
            required: true,
            message: "训练价格不能为空",
            trigger: "blur"
          },
          {
            pattern: /^[0-9]*$/,
            message: "训练价格只能是数字",
            trigger: "blur"
          }
        ]
      },
      rules1: {
        trainname: [
          {
            required: true,
            message: "训练名称不能为空",
            trigger: "blur"
          },
          {
            pattern: /^[\u4E00-\u9FA5]+$/,
            message: "训练只能为中文",
            trigger: "blur"
          }
        ],
        // clubid: [
        //   {
        //     required: true,
        //     message: "请选择属俱乐部",
        //     trigger: "change"
        //   }
        // ],
        trainaddress: [
          {
            required: true,
            message: "请填写训练地址",
            trigger: "blur"
          }
        ],
        intro: [
          {
            required: true,
            message: "训练简介不能为空",
            trigger: "blur"
          }
        ],
        traindate: [
          {
            type: "string",
            required: true,
            message: "请选择训练时间",
            trigger: "blur"
          }
        ],
        price: [
          {
            required: true,
            message: "训练价格不能为空",
            trigger: "blur"
          },
          {
            pattern: /^[0-9]*$/,
            message: "训练价格只能是数字",
            trigger: "blur"
          }
        ]
      },
      banCid: 0,
      onCid: 0,
      editVisible: false,
      clubid: localStorage.clubid
    };
  },
  created() {
    this.getData();
    this.gettrainclub();
    
  },
   computed: {
    data() {
      return this.trainData.filter(d => {
        if (
          d.trainname.indexOf(this.select_word) > -1 ||
          d.trainaddress.indexOf(this.select_word) > -1
        ) {
          return d;
        }
      });
    }
  },
  // mounted(){
  //   let _this = this;
  //     this.$root.$on("a1",(a2) => {
  //       this.$nextTick(function(){
  //       _this.getData();
  //       })
  //     })
  // },
  methods: {
    selectUserStatus() {
      if (this.select_status == -1) {
        this.getData();
      } else {
        this.getStatusData(this.select_status);
      }
    },
     piclimit(){
      // if (this.fileListEdit && this.fileListEdit.length > 0) {
      //       console.log(this.fileListEdit);
      //       this.$message.error("请删掉第一张图片");
      //     }
      this.$refs.upload.clearFiles();
    },
    handleSizeChange: function(size) {
      this.pagesize = size;
      console.log(this.pagesize); //每页下拉显示数据
    },
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage;
      // console.log(this.currentPage); //点击第几页
    },
// 分页
    newdate() {
      this.getData();
    },
    handleRemove(file, fileList) {
      // console.log("remove", file, fileList);
      this.fileList = fileList;
    },
    handleChange(file, fileList) {
      // console.log("777", file, fileList);
      this.fileList = fileList;
    },
    getStatusData(status){
        this.$axios.get('/api/Train/trainliststatus',{
        params: {
            status: status,
       }
      }).then(res => {
        this.trainData = res.data;
      })
     },
    getData() {
      this.$axios.get("/api/Train/trainlist",{
        params: {
          clubid: localStorage.clubid
        }
      }).then(response => {
        // console.log("response111111 :", response.data);
        this.trainData = response.data;
        this.$root.$emit("reset","ky");
      });

    },
    gettrainclub() {
      this.$axios.get("/api/Train/trainclub").then(response => {
        // console.log("response :", response.data);
        this.trainClub = response.data;
      });
    },
    search() {
      this.is_search = true;
    },
    filterTag(value, row) {
      return row.tag === value;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    onclickEdit(row) {
      this.editVisible = true;
      console.log("row=", row);
      this.form = JSON.parse(JSON.stringify(row));
      console.log("this.form=", this.form);
      this.fileListEdit = [];
      this.fileListEdit.push({
        name: "",
        url: "http://localhost/fitness/public/static/img/" + row.tpic
      });
    },
    saveEdit() {
      let vm = this;
      this.$refs["form"].validate(valid => {
        if (valid) {
          vm.$axios.post("/api/Train/edittrain", this.form).then(res => {
            console.log("res :", res.data);
          });
          vm.$refs.upload.submit();
          this.$message.success("修改成功");
          this.getData();
          this.editVisible = false;
        } else {
          this.$message.error("修改失败,请填写必填或正确信息");
          this.editVisible = true;
        }
      });
    },
    saveAdd() {
      let vm = this;
      this.$refs["addform"].validate(valid => {
        if (valid) {
          if (!(this.fileList && this.fileList.length > 0)) {
            console.log(this.fileList);
            this.$message.error("添加失败,请添加图片");
            return;
          }
          vm.$refs.upload.submit();
          this.$message.success("添加成功");
          this.getData();
          this.addVisible = false;
        } else {
          this.$message.error("添加失败,请填写必填信息");
          this.addVisible = true;
        }
      });
    },
    confirmBan() {
      this.$axios
        .get("/api/Train/bantrain", {
          params: {
            trainid: this.banTrainid
          }
        })
        .then(res => {
          console.log("res :", res.data);
          if (res.data.flag == 1) {
            this.$message.error("禁用成功");
            this.getData();
          }
        });
      this.banVisible = false;
    },
    confirmOn() {
      this.$axios
        .get("/api/Train/ontrain", {
          params: {
            trainid: this.onTrainid
          }
        })
        .then(res => {
          console.log("res :", res.data);
          if (res.data.flag == 1) {
            this.$message.success("启用成功");
            this.getData();
          }
        });
      this.onVisible = false;
    },
    handleBan(row) {
      this.banVisible = true;
      this.banTrainid = row.trainid;
    },
    handleOn(row) {
      this.onVisible = true;
      console, log(row);
      this.onTrainid = row.trainid;
    },
    editvisible(trainid) {
      this.editVisible = true;
      this.form.trainid = trainid;
    },
    handlePicsuccess(response) {
      console.log(response);
    }
  }
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
</style>
